<?php
namespace app\api\controller\v1;
use app\admin\controller\Reason;
use app\api\logic\GetAddressLogic;
use app\api\logic\QuanLogic;
use app\api\logic\SendMuLogic;
use app\service\GaodeService;
use app\service\MoneyLogService;
use app\service\OrderService;
use think\Db;
use app\service\UserWeixinService;
use app\service\TechnWeixinService;
use app\service\PosterService;
use app\service\TechnService;
use app\admin\logic\ZzLogic;
use app\service\PaymentService;
use think\Log;
use think\Request;


class Sm2 extends Base
{
	public function _initialize()
	{
		parent::_initialize();
	}
    /**
    定时统计在线技师
     */
    public function sm2(){
        printf("Versoin : %s\n", OPENSSL_VERSION_TEXT);

        $digests = openssl_get_md_methods(false);
        echo "Digests : ";
        foreach ($digests as $digest) {
            echo $digest.",";
        }
        echo "\n";

        $ciphers = openssl_get_cipher_methods(false);
        echo "Ciphers : ";
        foreach ($ciphers as $cipher) {
            echo $cipher.",";
        }
        echo "\n";

        $curves = openssl_get_curve_names();
        echo "Curves : ";
        foreach ($curves as $curve) {
            echo $curve.",";
        }
        echo "\n";
        echo "\n";

        $msg = "abc";
        printf("sm3(\"%s\") = %s\n", $msg, openssl_digest($msg, "sm3"));

        $key = openssl_random_pseudo_bytes(16);
        $ivlen = openssl_cipher_iv_length("sms4");
        $iv = openssl_random_pseudo_bytes($ivlen);
        $plaintext = "message to be encrypted";
        $ciphertext = openssl_encrypt($plaintext, "sms4", $key, $options=0, $iv);
        $original_plaintext = openssl_decrypt($ciphertext, "sms4", $key, $options=0, $iv);
        printf("sms4enc(\"%s\") = %s\n", $plaintext, bin2hex($ciphertext));
        printf("sms4dec(%s) = \"%s\"\n", bin2hex($ciphertext), $original_plaintext);

#$pubkey = openssl_pkey_get_public("file://localhost-signcer.pem");
#$prikey = openssl_pkey_get_private("file://localhost-signkey.pem");

        $prikey = openssl_pkey_new(array("private_key_type" => OPENSSL_KEYTYPE_EC, "curve_name" => "sm2p256v1"));
        openssl_pkey_export($prikey, $prikeypem);
        echo $prikeypem;
        $pubkeypem = openssl_pkey_get_details($prikey)["key"];
        echo $pubkeypem;
        $pubkey = openssl_pkey_get_public($pubkeypem);

        $point = openssl_pkey_get_details($pubkey)["ec"];
        printf("SM2 Public Key: (%s, %s)\n", bin2hex($point["x"]), bin2hex($point["y"]));
        $ec = openssl_pkey_get_details($prikey)["ec"];
        printf("SM2 Private Key: %s\n", bin2hex($ec["d"]));

        openssl_sign($msg, $signature, $prikey, "sm3");
        $ok = openssl_verify($msg, $signature, $pubkey, OPENSSL_ALGO_SM3);
        printf("sm2sign(\"%s\") = %s\n", $msg, bin2hex($signature));
        printf("sm2verify(\"%s\", %s) = %s\n", $msg, bin2hex($signature), $ok ? "OK" : "Failure");

        openssl_seal($plaintext, $sealed, $ekeys, array($pubkey), "sms4", $iv);
        openssl_open($sealed, $opened, $ekeys[0], $prikey, "sms4", $iv);
        printf("sm2seal(\"%s\") = %s\n", $plaintext, bin2hex($sealed));
        printf("sm2open(%s) = \"%s\"\n", bin2hex($sealed), $opened);
    }
    
}